﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using RenShiPinYongBiao.hrdbDataSetTableAdapters;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace RenShiPinYongBiao
{
    class DataSourceTech : DataSourcePinYong
    {
        /************************************************************************/
        /* dt 是从Excel文件取得                                                 */
        /************************************************************************/
        public DataSourceTech(DataTable dt):base(dt)
        {
            
        }
        public override List<string> PrepareText(int index)
        {
            List<string> texts = new List<string>();
            DataRow row = dt.Rows[index];
            ConnectionManager.OpenConnection();
            SqlCommand cmd=ConnectionManager.Connection.CreateCommand();
            cmd.CommandText = "select * from TEmployee where FEmployeeID=@id";
            cmd.Parameters.AddWithValue("id", row["档案号"]);
            SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            dr.Read();
            //texts.Add("姓名");
            texts.Add(dr["FName"].ToString());
            //texts.Add("性别");
            texts.Add(FindNameByCode(dr["FGenderCode"], this.genderDict));
            //texts.Add("民族");
            texts.Add(FindNameByCode(dr["FNationCode"], this.nationDict));
            //texts.Add("出生年月");
            texts.Add(FormatDate(dr["FBirthday"]));
            //texts.Add("参加工作时间");
            texts.Add(this.FormatDate(dr["FFirstWorkDate"]));
            //texts.Add("政治面貌");
            texts.Add(FindNameByCode(dr["FPartyCode"], this.partyDict));
            texts.Add(row["出生地"].ToString());
            texts.Add("健康");
            texts.Add(row["全日制教育"].ToString());
            texts.Add(row["毕业学校及专业q"].ToString());
            texts.Add(row["在职教育"].ToString());
            texts.Add(row["毕业学校及专业z"].ToString());
            texts.Add(row["聘前岗位职务或职称"].ToString());
            texts.Add(row["聘任岗位职务或职称"].ToString());
            texts.Add(row["聘期"].ToString());

            DataTable wsdt = new TWorkStoryTableAdapter().GetDataById(dr["FEmployeeID"].ToString());
            bool has = false;
            if (wsdt.Rows.Count > 0)
            {
                has = true;
            }
            StringBuilder sb = new StringBuilder();
            if (has)
            {

                for (int i = wsdt.Rows.Count - 1; i >= 0; i--)
                {
                    DataRow r = wsdt.Rows[i];

                    sb.Append(this.FormatDate(r["FStartDate"], true));
                    sb.Append("--");
                    sb.Append(this.FormatDate(r["FEndDate"], true));
                    sb.Append("");
                    sb.Append(r["FCompany"]);
                    sb.Append("   ");
                    sb.Append(r["FHeadShip"].Equals(DBNull.Value) ? "      " : r["FHeadShip"]);
                    sb.Append("\r\n");
                }
            }
            texts.Add(has ? sb.ToString() : "");
            has = false;

            sb = new StringBuilder();
            DataTable hdt = new THouseHoldTableAdapter().GetDataById(dr["FEmployeeID"].ToString());
            foreach (DataRow row1 in hdt.Rows)
            {
                sb.Append(row1["FRelation"]);
                sb.Append("   ");
                sb.Append(row1["FName"]);
                sb.Append("   ");
                sb.Append(row1["FCompany"]);
                sb.Append("   ");
                sb.Append(row1["FHeadShip"]);
                sb.Append("\r\n");
            }
            texts.Add(sb.ToString());
            //添加照片
            if(photo!=null)
                photo.Dispose();
            photo = null;
            if (!dr["FPhoto"].Equals(System.DBNull.Value))
            {
                try
                {
                    photo = ImageHelp.CutForCustom(Image.FromStream(new MemoryStream((Byte[])dr["FPhoto"])),90,135);
                }
                catch (Exception)
                {
                }
            }
            dr.Close();
            return texts;
        }
    }
}
